﻿// RemoteCtrl.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include "framework.h"
#include "RemoteCtrl.h"
#include "ServerSocket.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

int main()
{
    int nRetCode = 0;

    HMODULE hModule = ::GetModuleHandle(nullptr);

    if (hModule != nullptr)
    {     
        if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0))
        {
            wprintf(L"错误: MFC 初始化失败\n");
            nRetCode = 1;
        }
        else
        {
            if (pserver->InitSocket(9999) == FALSE)
            {
                AfxMessageBox(L"加载winsocket失败");
                exit(0);
            }
            int count = 0;
            while (pserver != NULL)
            {
                if (pserver->AcceptClient() == FALSE)
                {
                    if (count >= 3)
                    {
                        AfxMessageBox(L"超出重试，程序退出");
                        exit(0);
                    }
                    count++;
                    AfxMessageBox(L"连接客户端失败，自动重试");
                }
                int ret = pserver->DealCommand();
            }
           
        }
    }
    else
    {
        wprintf(L"错误: GetModuleHandle 失败\n");
        nRetCode = 1;
    }

    return nRetCode;
}
